/*  2023/03/23 by DPH
*   在应用层实现勤务帧的功能
*   
*
*
*/

#ifndef SERVEFRAME_H
#define SERVEFRAME_H

#include "ns3/application.h"
#include "ns3/traced-value.h"
#include "serve-header.h"
#include <list>



namespace ns3{

class Socket;
class Packet;

class ServeFrame : public Application
{
public:

    // 设置属性
    static TypeId GetTypeId (void);

    // 构造析构函数
    ServeFrame ();
    ServeFrame(Ptr<NetDevice> netDevice);
    //ServeFrame (Ptr<NetDevice> netDevice);
    virtual ~ServeFrame ();

    // 获取绑定的网卡
    Ptr<NetDevice> GetServeNetDevice (void);
    void SetServeNetDevice(Ptr<NetDevice> netDevice);

    //
    

private:
    enum Status
    {
        COMPETING_CENTER = 1, //竞争中心节点中
        STOP_COMPETING = 2,
    };


    static const int SERVE_PORT = 65;

    virtual void StartApplication (void);
    virtual void StopApplication (void);
    void LinkStateHandler (void);
    void NetHandler (Ptr<Socket> socket);
    void RequCenterHander(ServeHeader header);
    void Boot(void);
    void SendRequCenter (void);
    void StopCompete (void);

    uint8_t m_state;            //建网的状态 
    bool m_firstBoot;
    Ptr<NetDevice> m_device;
    Ptr<Socket> m_socket;
    Ipv4Address m_myAddress;    // 自己的I🍕v4地址
    Ipv4Address m_poolAddress;             //!< The network address available to the server
    Ipv4Address m_minAddress;              //!< The first address in the address pool
    Ipv4Address m_maxAddress;              //!< The last address in the address pool


    Address m_chaddr;
    
    EventId m_finishCompete;    // 结束竞争，进行状态转化
    EventId m_requCenterEvent;  // 没有说到同步帧或者id小 重新广播自举中心勤务帧


    Time m_rtrs;                // 竞争中心勤务帧重传时间
    Time m_compete;             // 组网时间，这个时间到结束组网  
};






}//namespace ns3





#endif